<?php
	header('Access-Control-Allow-Origin:*');
    $type = $_GET['type'];
    $uid = $_GET['uid'];
    $con = new mysqli("127.0.0.1", "root", "", "accounting");
    $con->query("SET NAMES 'utf8'");

    //查询所有账单
    if($type == "selectAllAccount"){

        $selectSQL = "SELECT * FROM `account_book` WHERE `uid` = '$uid' ORDER BY `aid` DESC";
        $result = $con->query($selectSQL);
        $results = array();
        while ($line = mysqli_fetch_assoc($result)) {
            array_push($results,$line);
        }
        echo json_encode($results);
    }
    //查询最近7日
    if($type == "selectLastAccount"){
        $selectSQL = "SELECT * FROM `account_book` WHERE `uid` = '$uid' limit 7";
        $result = $con->query($selectSQL);
        $results = array();
        while ($line = mysqli_fetch_assoc($result)) {
            array_push($results,$line);
        }
        echo json_encode($results);
    }
    //添加
    if($type == "add"){
        $accountType = $_GET["accountType"];
        $amount = $_GET["amount"];
        $remark = $_GET["remark"];
        $iconType = $_GET["iconType"];
        $time = $_GET["time"];

        $addSQL = "INSERT INTO `accounting`.`account_book` (`aid`, `type`, `time`, `amount`, `remark`, `uid`, `iconType`) VALUES (NULL, '$accountType', '$time', '$amount', '$remark', '$uid', '$iconType')";
        $result = $con->query($addSQL);
        // while ($line = mysqli_fetch_assoc($result)) {
        //     array_push($results,$line);
        // }
        echo json_encode($result);
    }
    //删除
    if($type == "delete"){
        $aid = $_GET['aid'];
        $delSQL = "DELETE FROM `accounting`.`account_book` WHERE `account_book`.`aid` = '$aid'";
        $result = $con->query($delSQL);

        echo json_encode($result);
    }
    //更新
    if($type == "update"){
        $aid = $_GET['aid'];
        $accountType = $_GET["accountType"];
        $amount = $_GET["amount"];
        $remark = $_GET["remark"];
        $iconType = $_GET["iconType"];

        $delSQL = "UPDATE `accounting`.`account_book` SET `type` = '$accountType', `amount` = '$amount', `remark` = '$remark', `iconType` = '$iconType' WHERE `account_book`.`aid` = $aid";
        $result = $con->query($delSQL);

        echo json_encode($result);
    }
?>